package com.ymm.utils.system;

import com.ymm.domain.entity.SystemState;
import com.ymm.domain.vo.OSFileStoreVo;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
import oshi.util.FormatUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 椰咩咩咩
 * @version 1.0-SNAPSHOT
 * @date 2023/2/1 15:33
 * @Description
 */
public class SystemUtils {

    public static SystemState getSystemState() {
        SystemState systemState = new SystemState();
        //系统信息
        SystemInfo info = new SystemInfo();
        //操作系统信息
        OperatingSystem operatingSystem = info.getOperatingSystem();
        //硬件信息
        HardwareAbstractionLayer hardware = info.getHardware();
        //内存相关信息
        GlobalMemory memory = hardware.getMemory();
        //获取内存总容量
        String totalMemory = FormatUtil.formatBytes(memory.getTotal());
        systemState.setTotalMemory(totalMemory);

        //获取可用内存的容量
        String availableMemory = FormatUtil.formatBytes(memory.getAvailable());
        systemState.setAvailableMemory(availableMemory);

        //CPU相关信息
        CentralProcessor processor = hardware.getProcessor();
        //获取CPU名字
        String processorName = processor.getProcessorIdentifier().getName();
        systemState.setProcessorName(processorName);

        //获取物理CPU数
        int physicalPackageCount = processor.getPhysicalPackageCount();
        systemState.setPhysicalPackageCount(physicalPackageCount);

        //获取物理核心数
        int physicalProcessorCount = processor.getPhysicalProcessorCount();
        systemState.setPhysicalProcessorCount(physicalProcessorCount);

        //操作系统系列
        String osFamily = operatingSystem.getFamily();
        systemState.setOsFamily(osFamily);

        //操作系统位数
        int bitness = operatingSystem.getBitness();
        systemState.setBitness(bitness);

        //制造商
        String manufacturer = operatingSystem.getManufacturer();
        systemState.setManufacturer(manufacturer);

        //文件系统
        FileSystem fileSystem = operatingSystem.getFileSystem();
        //存储实例化对象
        List<OSFileStore> fileStores = fileSystem.getFileStores();

        List<OSFileStoreVo> osFileStoreVos = fileStores.stream()
                .map(osFileStore -> {
                    OSFileStoreVo osFileStoreVo = new OSFileStoreVo();
                    osFileStoreVo.setName(osFileStore.getName());
                    osFileStoreVo.setDescription(osFileStore.getDescription());
                    osFileStoreVo.setType(osFileStore.getType());
                    osFileStoreVo.setFreeSpace(FormatUtil.formatBytes(osFileStore.getFreeSpace()));
                    osFileStoreVo.setTotalSpace(FormatUtil.formatBytes(osFileStore.getTotalSpace()));
                    return osFileStoreVo;
                }).collect(Collectors.toList());

        systemState.setFileStores(osFileStoreVos);

        return systemState;
    }
}
